package com.xtaller.lazy.lib.generator;

import com.xtaller.lazy.lib.kit.TimeUtil;
import lombok.var;

import java.sql.ResultSet;
import java.sql.SQLException;

/**   
 * @author: Taller   
 * @date:   Mar 12, 2019 3:28:57 PM   
 * @version V1.0 
 * @Description: 生成绑定在Mapping中的实体
 */
public class Code {
	// 创建mapping映射的实体
	public static StringBuffer createModel(
			String module, 
			String model,
			String tableName,
			int tableId,
			String description,
			ResultSet column) {
		StringBuffer sb = new StringBuffer();
		sb.append("package com.xtaller.lazy.model." + module + ";\n\n");
		sb.append("import com.baomidou.mybatisplus.annotation.TableField; \n");
		sb.append("import com.baomidou.mybatisplus.annotation.TableName; \n");
		sb.append("import com.xtaller.lazy.lib.annotation.Table; \n");
		sb.append("import lombok.Data; \n");
		sb.append("import lombok.Data; \n");
		sb.append("/** \n");
		sb.append("* 代码生成 \n");
		sb.append("* @author: Taller  \n");
		sb.append("* @date: " + TimeUtil.formatNow("yyyy/MM/dd") + "\n");
		sb.append("* @Description: " + description + " \n");
		sb.append("*/ \n");
		sb.append("@Data \n");
		sb.append("@TableName(value = \"" + tableName + "\") \n");
		sb.append("@Table(id = " + tableId + ") \n");
		sb.append("public class " + model +" { \n");
		try {
			while (column.next()) {
				sb.append(createGetSet(column.getString(2), column.getString(1), column.getString(3)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sb.append("} \n");
		return sb;
	}
	
	// 创建Mapper
	public static StringBuffer createMapper(String module,
											String model,
											String description){
		StringBuffer sb = new StringBuffer();
		sb.append("package com.xtaller.lazy.mapper." + module + ";\n\n");
		sb.append("import com.baomidou.mybatisplus.core.mapper.BaseMapper; \n");
		sb.append("import com.xtaller.lazy.model." + module + "." + model + "; \n");
		sb.append("/** \n");
		sb.append("* 代码生成 \n");
		sb.append("* @author: Taller  \n");
		sb.append("* @date: " + TimeUtil.formatNow("yyyy/MM/dd") + "\n");
		sb.append("* @Description: " + description + " \n");
		sb.append("*/ \n");

		sb.append("public interface " + model +"Mapper extends BaseMapper<"+ model +"> { \n");
		sb.append("} \n");
		return sb;
	}
	
	private static StringBuffer createGetSet(String type, String field, String context) {
		StringBuffer sb = new StringBuffer();
		sb.append("    //@ApiModelProperty(value = \"" + context +"\") \n");
		sb.append("    @TableField(value = \"" + field + "\") \n");
		if(type.equals("tinyint") || 
				type.equals("smallint") || 
				type.equals("int")) {
			sb.append("    private int " + field + ";\n");

		}else if(type.equals("bigint")) {
			sb.append("    private Long " + field + ";\n");
		}else if(type.equals("datetime")) {
			
		}else {	// varchar text
			sb.append("    private String " + field + ";\n");
		}
		sb.append("\n");
		return sb;
	}
	
	// 创建服务类
	public static StringBuffer createService(
			String module,
			String model,
			String description
			) {
		StringBuffer sb = new StringBuffer();
		sb.append("package com.xtaller.lazy.service." + module + "; \n\n");

		sb.append("import com.xtaller.lazy.base.TServiceImpl; \n");
		sb.append("import com.xtaller.lazy.mapper."+ module + "."+ model + "Mapper; \n");
		sb.append("import com.xtaller.lazy.model."+ module + "."+ model + "; \n");
		sb.append("import org.springframework.stereotype.Service; \n");
		sb.append(" \n");
		sb.append("/** \n");
		sb.append("* 代码生成 \n");
		sb.append("* @author: Taller  \n");
		sb.append("* @date: " + TimeUtil.formatNow("yyyy/MM/dd") + "\n");
		sb.append("* @Description: " + description + "服务 \n");
		sb.append("*/ \n");
		sb.append("@Service \n");
		sb.append("public class " + model + "Service extends TServiceImpl<" + model + "Mapper, "+model+">{ \n");
		sb.append("}\n");
		
		return sb;
	}

	private static String formatField(String field){
        var fileName = field.substring(0, 1).toUpperCase() + field.substring(1, field.length());
        return fileName;
    }
}
